<?php
/**
 * @abstract: Xem profile
 * @author: HoanVX
 * @version $Id: profile.php 85 2015-03-15 15:21:26Z voxuanhoan@gmail.com $
 */

/*
 * Template Name: Profile
 */

// Inluce lib
require_once (ABS_THEME_PATH .'helper/dbtransaction.php');


// Only display after login
// Variable
$loginUser 		= "";
$firstName 		= "";
$lastName 		= "";
$phone 			= "";
$address		= "";
$email			= "";
$facebook 		= "";
$skype			= "";
$company		= "";
$oldpassword	= "";
$newPassword	= "";
$reNewPassword	= "";

if (!is_user_logged_in()) {
	wp_redirect(get_the_permalink(33));
}
$currentUser = wp_get_current_user();

// Validate form
$validate = new Validation();

if (isset($_POST['doSave'])) {

	$firstName	= ChotroiUtils::convertPostNullToBlank('txtFirstName');
	$lastName 	= ChotroiUtils::convertPostNullToBlank('txtLastName');
	$phone		= ChotroiUtils::convertPostNullToBlank('txtPhone');
	$address	= ChotroiUtils::convertPostNullToBlank('txtAddress');
	$email		= ChotroiUtils::convertPostNullToBlank('txtEmail');
	$facebook	= ChotroiUtils::convertPostNullToBlank('txtFacebook');
	$skype		= ChotroiUtils::convertPostNullToBlank('txtSkype');
	$oldpassword = ChotroiUtils::convertPostNullToBlank('txtOldPassword');
	$newPassword = ChotroiUtils::convertPostNullToBlank('txtNewPassword');
	$reNewPassword = ChotroiUtils::convertPostNullToBlank('txtReNewPassword');
	$company	= ChotroiUtils::convertPostNullToBlank('txtCompany');

	// Check first name
	if(empty($firstName))
		$validate->setError(array('itemName'=>'txtFirstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_REQUIRED));
	if(strlen($firstName) < 2) {
		$validate->setError(array('itemName'=>'txtFirstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_MINLENGTH));
	}
	if(strlen($firstName) > 20) {
		$validate->setError(array('itemName'=>'txtFirstName', 'message'=>Messages::ERROR_REGISTER_FIRSTNAME_MAXLENGTH));
	}

	// Check last name
	if (empty($lastName)) {
		$validate->setError(array('itemName'=>'txtLastName', 'message'=>Messages::ERROR_REGISTER_LASTNAME_REQUIRED));
	}
	if(strlen($lastName) > 10 ) {
		$validate->setError(array('itemName'=>'txtLastName', 'message'=>Messages::ERROR_REGISTER_LASTNAME_MAXLENGTH));
	}

	// Check phone
	if(!empty($phone)) {
		if(!is_numeric($phone))
			$validate->setError(array('itemName'=>"txtPhone", 'message'=>Messages::ERROR_REGISTER_PHONE_NUMBER));
		if(strlen($phone) < 10)
			$validate->setError(array('itemName'=>"txtPhone", 'message'=>Messages::ERROR_REGISTER_PHONE_MINLENGTH));
		if(strlen($phone) > 15)
			$validate->setError(array('itemName'=>"txtPhone", 'message'=>Messages::ERROR_REGISTER_PHONE_MAXLENGTH));
	}

	// Check Email
	if(empty($email))
		$validate->setError(array('itemName'=>'txtEmail', 'message'=>Messages::ERROR_COMMON_EMAIL_REQUIRED));
	if (strlen($email) > 100)
		$validate->setError(array('itemName'=>'txtEmail', 'message'=>Messages::ERROR_COMMON_EMAIL_MAXLENGTH));
	if(!is_email($email))
		$validate->setError(array('itemName'=>'txtEmail', 'message'=>Messages::ERROR_COMMON_EMAIL_INVALID));

	// Check dia chi
	if(!empty($address) && strlen($address) > 150)
		$validate->setError(array('itemName'=>'txtAddress', 'message'=>Messages::ERROR_REGISTER_ADDRESS_MAXLENGTH));

	// Check facebook
	if(!empty($facebook) && strlen($facebook) > 50)
		$validate->setError(array('itemName'=>'txtFacebook', 'message'=>Messages::ERROR_REGISTER_FACEBOOK_MAXLENGTH));

	// Check facebook
	if(!empty($skype) && strlen($skype) > 50)
		$validate->setError(array('itemName'=>'txtSkype', 'message'=>Messages::ERROR_REGISTER_SKYPE_MAXLENGTH));

	// Check company
	if(!empty($company) && strlen($company) > 200)
		$validate->setError(array('itemName'=>'txtCompany', 'message'=>Messages::ERROR_REGISTER_COMPANY_MAXLENGTH));

	// Check password
	// If one of a password textbox is inputed
	if (!empty($oldpassword) || !empty($newPassword) || !empty($reNewPassword)) {

		// Check old password
		if(empty($oldpassword)) {
			$validate->setError(array('itemName'=>'txtOldPassword', 'message'=>Messages::ERROR_PROFILE_OLD_PASSWORD_REQUIRED));
		}
		if (!wp_check_password($oldpassword, $currentUser->user_pass, $currentUser->ID)) {
			$validate->setError(array('itemName'=>'txtOldPassword', 'message'=>Messages::ERROR_PROFILE_OLD_PASSWORD_MATCH));
		}

		// Check for new password
		if(empty($newPassword)) {
			$validate->setError(array('itemName'=>'txtNewPassword', 'message'=>Messages::ERROR_PROFILE_NEW_PASSWORD_REQUIRED));
		}
		if(strlen($newPassword) < 6)
			$validate->setError(array('itemName'=>'txtNewPassword', 'message'=>Messages::ERROR_COMMON_PASSWORD_MINLENGTH));
		if(strlen($newPassword) > 20)
			$validate->setError(array('itemName'=>'txtReNewPassword', 'message'=>Messages::ERROR_COMMON_PASSWORD_MAXLENGTH));
		if(strcasecmp($newPassword, $reNewPassword) != 0) {
			$validate->setError(array('itemName'=>'txtReNewPassword', 'message'=>Messages::ERROR_REGISTER_PASSWORD_MAPPING));
		}
	}

	if(!$validate->hasError()) {
		$transaction = new DBTransaction();
		try {
			$transaction->beginTransaction();

			$userdata = array(
					'ID'			=> $currentUser->ID,
					'user_email' 	=> $email,
					'first_name' 	=> $firstName,
					'last_name' 	=> $lastName,
					'display_name'	=> $firstName . " " . $lastName,
			);

			if (!empty($newPassword)) {
				$userdata['user_pass'] = $newPassword;
			}

			$user_id = wp_update_user( $userdata );

			if ( ! $user_id || is_wp_error( $user_id ) ) {

				foreach ($user_id->get_error_codes() as $key) {
					if(strcmp($key, "existing_user_login") == 0 || (strcmp($key, "existing_user_email") == 0)) {
						$validate->setError(array('itemName'=>'email', 'message'=>Messages::ERROR_COMMON_EMAIL_EXISTED));
					}
				}
				goto displayHTML;
			}

			// Convert to blank if its value is blank
			$phone = empty($phone) ? "" : $phone;
			$address = empty($address) ? "" : $address;
			$facebook = empty($facebook) ? "" : $facebook;
			$skype = empty($skype) ? "" : $skype;
			$company = empty($company) ? "" : $company;

			// Add meta data : prefix: ct:  cho troi
			// phone
			update_user_meta($user_id, CT_PHONE, $phone);
			// address
			update_user_meta($user_id, CT_ADDRESS, $address);
			// facebook
			update_user_meta($user_id, CT_FACEBOOK, $facebook);
			//skype
			update_user_meta($user_id, CT_SKYPE, $skype);
			// company
			update_user_meta($user_id, CT_COMPANY, $company);

			// Commit transaction
			$transaction->commit();

		} catch (Exception $e) {
			$transaction->rollBack();
		}
	}
	// Update user's information
} else {

	$firstName = $currentUser->first_name;
	$lastName  = $currentUser->last_name;
	$address   = get_the_author_meta(CT_ADDRESS, $currentUser->ID);
	$phone	   = get_the_author_meta(CT_PHONE, $currentUser->ID);
	$email	   = $currentUser->user_email;
	$facebook  = get_the_author_meta(CT_FACEBOOK, $currentUser->ID);
	$skype	   = get_the_author_meta(CT_SKYPE, $currentUser->ID);
	$company   = get_the_author_meta(CT_COMPANY, $currentUser->ID);
}

displayHTML:

add_style('css/page/profile.css');
add_script("js/jquery.validate.js");
add_script('js/page/profile.js');
get_header();

?>
<!-- header-section-ends -->
<!-- content-section-starts -->
<div class="content">
	<div class="container">

		<div role="tabpanel">
  			<!-- Nav tabs -->
	  		<ul class="nav nav-tabs" role="tablist">
			    <li role="presentation" class="active"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
			    <li role="presentation"><a href="<?php echo get_the_permalink(176);?>" aria-controls="managerInfo" role="tab" data-toggle="">Quản lý thông tin rao</a></li>
			    <li role="presentation"><a href="<?php echo get_the_permalink(177);?>" aria-controls="infoSaved" role="tab" data-toggle="">Danh sách tin đã lưu</a></li>
		  	</ul>

		  	<!-- Tab panes -->
			<div class="tab-content">
				<div role="tabpanel" class="tab-pane active" id="profile">
					<div id="left">
						<form action="" id="profileForm" method="POST">
							<table class="table table-hover">
								<tr>
						  			<th class="info">Tên đăng nhập:</th>
						  			<td><?php echo $currentUser->user_login; ?></td>
						  		</tr>
						  		<tr>
						  			<th class="warning" colspan="2">Thay thông tin cá nhân</th>
						  		</tr>
						  		<tr>
						  			<th class="info">Họ đệm <span class="cr"><b>(*)</b></span>:</th>
						  			<td>
						  				<input type="text" name="txtFirstName" value="<?php echo ucwords($firstName);?>" class="form-control" placeholder="Họ và tên đệm"/>
						  				<?php echo $validate->getError('txtFirstName');?>
						  			</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Tên <span class="cr"><b>(*)</b></span>:</th>
						  			<td>
						  				<input type="text" name="txtLastName" value="<?php echo ucwords($lastName);?>"  class="form-control" placeholder="Tên"/>
						  				<?php echo $validate->getError('txtLastName');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Điện thoại</th>
						  			<td>
						  				<input type="text" name="txtPhone" value="<?php echo $phone; ?>" class="form-control" placeholder="Điện thoại của bạn"/>
						  				<?php echo $validate->getError('txtPhone');?>
						  			</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Địa chỉ:</th>
						  			<td>
						  				<input type="text" name="txtAddress" value="<?php echo ucwords($address);?>"  class="form-control" placeholder="Nhập địa chỉ"/>
						  				<?php echo $validate->getError('txtAddress');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Email <span class="cr"><b>(*)</b></span>:</th>
						  			<td>
						  				<input type="text" name="txtEmail" value="<?php echo ucwords($email);?>"  class="form-control" placeholder="Nhập email"/>
						  				<?php echo $validate->getError('txtEmail');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Facebook:</th>
						  			<td>
						  				<input type="text" name="txtFacebook" value="<?php echo $facebook; ?>"  class="form-control" placeholder="Tài khoản facebook của bạn"/>
					  					<?php echo $validate->getError('txtFacebook');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Skype:</th>
						  			<td>
						  				<input type="text" name="txtSkype" value="<?php echo $skype; ?>"  class="form-control" placeholder="Tài khoản skype của bạn"/>
						  				<?php echo $validate->getError('txtSkype');?>
					  				</td>
						  		</tr>
					  			<tr>
						  			<th class="info">Công ty:</th>
						  			<td>
						  				<input type="text" name="txtCompany" value="<?php echo $company; ?>"  class="form-control"	 placeholder="Tên công ty của bạn"/>
						  				<?php echo $validate->getError('txtCompany');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="warning" colspan="2">Thay đổi mật khẩu</th>
						  		</tr>
						  		<tr>
						  			<th class="info">Mật khẩu cũ:</th>
						  			<td>
						  				<input type="password" name="txtOldPassword"  class="form-control" placeholder="Nhập lại mật khẩu cũ"/>
					  					<?php echo $validate->getError('txtOldPassword');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Mật khẩu mới:</th>
						  			<td>
						  				<input type="password" name="txtNewPassword"  class="form-control" placeholder="Nhập mật khẩu mới"/>
					  					<?php echo $validate->getError('txtNewPassword');?>
					  				</td>
						  		</tr>
						  		<tr>
						  			<th class="info">Nhập lại mật khẩu mới:</th>
						  			<td>
						  				<input type="password" name="txtReNewPassword"  class="form-control" placeholder="Nhập lại mật khẩu mới"/>
					  					<?php echo $validate->getError('txtReNewPassword');?>
					  				</td>
						  		</tr>
					  			<tr>
						  			<td colspan="2"><input type="submit" name="doSave" class="form-control btn-primary"	value="Lưu"/> </td>
						  		</tr>
							</table>
						</form>
					</div>
					<div id="right">
						<h2 class="bg-primary">Lưu ý</h2>
						<div class="noteContent">
						<?php
						if (have_posts()): the_post();
							the_content();
						endif;
						?>
						</div>

					</div>
				</div>
			    <div role="tabpanel" class="tab-pane" id="managerInfo">...</div>
			    <div role="tabpanel" class="tab-pane" id="infoSaved">.infoSaved.</div>
		  	</div>
		</div>
	</div>

<!-- Contact's information -->
<?php get_footer("contact");?>
<!-- END Contact's information -->
</div>
<!-- content-section-ends -->
<!-- footer-section-starts -->
<?php get_footer();?>